package com.ncepu.thumb.warmup;

import com.ncepu.thumb.model.entity.Blog;
import com.ncepu.thumb.service.BlogService;
import com.ncepu.thumb.utils.RedisKeyUtil;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * 缓存预热
 */
@AllArgsConstructor
@Component
public class BlogCacheWarmUp {

    private final BlogService blogService;
    private final RedisTemplate<String, Object> redisTemplate;

    @PostConstruct
    public void warmUpCache() {
        // 从数据库中读取所有博客的 ID
        List<Long> blogIds = blogService.lambdaQuery()
                .select(Blog::getId)
                .list()
                .stream()
                .map(Blog::getId)
                .toList();

        // 将所有博客 ID 添加到 Redis 的 Set 中
        String blogsExistedKey = RedisKeyUtil.getBlogsExistedKey();
        redisTemplate.opsForSet().add(blogsExistedKey, blogIds.toArray());

    }
}